import {
    ref,
    computed // 计算属性
} from 'vue'

export function useTodos() {
    let title = ref('')
    let todos = ref([
        {
            id: 1,
            title: '吃饭',
            done: false
        }, {
            id: 2,
            title: '睡觉',
            done: false
        }, {
            id: 3,
            title: '打豆豆',
            done: false
        }
    ])

    function addTodo() {
        todos.value.push({
            id: Date.now(),
            title: title.value,
            done: false
        })
        title.value = '';
    }

    function clear() {
        // done false 留下，已完成的清除
        todos.value = todos.value.filter(item => !item.done)
    }
    // 未完成的todos 的数量
    let active = computed(() => {
        return todos.value.filter(item => !item.done).length
    })
    let all = computed(() => todos.value.length)
    let allDone = computed({
        get: function() {
            return active.value === 0
        },
        set: function(value) {
            todos.value.forEach(item => item.done = value)
        }
    })

    return {
        title,
        addTodo,
        clear,
        todos,
        active,
        all,
        allDone
    }
}